<?php
class setting_option extends MX_Controller{
	public function index(){
		$adminMenu= $this->load->module('adminMenu/index');
		$setting =$this->load->Module('setting/admin');
		$this->template=$this->config->config['admin_template'];
		if(isset($_POST)){
			foreach ($_POST as $key=>$value){
				if(!is_array($value))$data[$key]=trim($value);
				else $data[$key]=$value;
			}
		}
		if(isset($_POST['Add_Options'])){
			unset($data['Add_Options']);
			$this->model()->add_options($data);
		}
		
		if(isset($_POST['Update_Options'])){
			unset($data['Update_Options']);
			$this->model()->update_options($data);
		}
		
		
		$search='';
		if(isset($_POST['search_option'])){
			$search=$_POST['search_option'];
		}
		
		$OptionBody=$this->model()->listOption($search);
		$optionParent=$this->model()->listOptionParent();
		$data=array(
					'update_footer'=>'test',
					'admin_title'=>'Control Admin',
					'option_category'=>array(),
					'admin_menu'=>$adminMenu->generateAdminMenu(),
					'tableOptionBody'=>$OptionBody,
					'optionParent'=>self::dropList($optionParent)
				);
		
		
		$this->view(
				array('admin-header',
						'setting-option',
						'admin-footer'), 
				$data); // Display the page
	}
	public function delete($id){
		
		$return=$this->model()->delete($id);
		
		$this->load->helper('url');
		redirect('setting/option');
	}
	private function printListOption($lists=array(),$option=array(),$showPrice=true){
		$out=$min=$max='';
		$this->load->library('session');
		
		$price=$this->session->userdata('price');
		if(isset($price['min']))$min=$price['min'];
		if(isset($price['max']))$max=$price['max'];
		
		if($showPrice) {
			$out.= "
			<div class='control-group '>
				<label for='option_price' class='control-label span5'>Prix Entre</label>
				<div class='controls'> &nbsp; &nbsp; &nbsp; ";
			$out.= "
					 <input  id='option_price' name='priceMin' value='$min' placeholder='prix min' class='span1'/>
						et  
					  <input  id='option_price' name='priceMax' value='$max' placeholder='prix max' class='span1'/>";
			$out.= '</div>
			</div>
			';
		}
		
		if(!empty($lists))
		foreach ($lists as $list){
			$name=$list->name;
			$exp=explode(' - ',$list->name);
			if(isset($exp[1]))$name=$exp[1];
				
			$out.= "
			<div class='control-group span5'>
			<label for='option_{$list->id_type}' class='control-label'>{$name}</label>
				<div class='controls controls-row'>";
			if($list->type=='select'){
		
				$out.= " <select id='option_{$list->id_type}' name='option[{$list->id_type}]'>
						<option> </option>
					";
				foreach (explode(',',$list->value) as $value){
					$s='';
					if( isset($option[$list->id_type]) and $option[$list->id_type]==$value)
					$s=' selected="selected" ';
					$out.="<option value='$value' $s>$value</option>";
				}
				$out.= " </select>
				";
			}else{
				$value=$list->value;
				if(isset($option[$list->id_type]))$value=$option[$list->id_type];
				$out.= "<input  id='option_{$list->id_type}' name='option[{$list->id_type}]' value='$value'/>\n";
			}
			if($list->unit) $out.= "<span class='help-inline'>{$list->unit}</span>";
			$out.= '</div>
			</div>
			';
		}
		return $out;
	}
	public function getOptions($id_options='',$option=array(),$viewHtml=false){
		if(isset($_POST['idOption'])) $id_options=$_POST['idOption'];
		$out='';
		if(is_numeric($id_options)){
			$lists=$this->model()->get_options($id_options);
			if(!empty($lists))
				foreach ($lists as $index=>$list)
					if($list->parent>0) unset($lists[$index]);
					//check si il permet de voir le prix
			$showPrice=true;
			$noAllowPrice=$this->model('category_models_index')->not_showPrice();
			if(in_array($id_options,$noAllowPrice))$showPrice=false;
					
			$out.=self::printListOption($lists,$option,$showPrice);
		}
		
		if($viewHtml)return $out;
		
		if($out) echo $out;
		
		return false;
	}
	public function getFilters($id_options='',$option=array(),$viewHtml=false){
		if(isset($_POST['idOption'])) $id_options=$_POST['idOption'];
		$out='';
		if(!empty($id_options)){
			//check si il permet de voir le prix
			$showPrice=true;
			$noAllowPrice=$this->model('category_models_index')->not_showPrice();
			if(in_array($id_options,$noAllowPrice))$showPrice=false;
			
			$lists=$this->model()->get_Filters($id_options);
			$out.=self::printListOption($lists,$option,$showPrice);
		}

		if($viewHtml)return $out;
		
		if($out) echo $out;
		
		return false;
	}
	private function dropList($ListOptions,$idOptionSelected=''){
		$out='';
		foreach ($ListOptions as $option){
			$s='';
			if($idOptionSelected and $idOptionSelected==$option->id_type)
				$s=' selected="selected" ';
			$out.="<option value='{$option->id_type}' $s>{$option->name}</option>";
		}
		if($out) return $out;
		
		return false;
	}
}